<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		errorModal,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		hasEditor,
		getEditorTitle,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				infoCacheKey:"new-kq-back-info-cache",
				dvids:""
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		watch:{
			dvids:function(newVal,oldVal){
				this.getChildData(newVal)
			}
		},
		methods:{
			getEmpBasicInfo(userId){
				let _that = this
				_that.om.request(
					"temployee/getEmpBasicInfo",
					{
						userId:userId
					}
				).then(res=>{
					let formdata = _that.$refs.autoform.formdata
					let d = res.value
					formdata.sexName = d.sexName
					formdata.birthdate = d.birthdate
					formdata.birthPlace = d.birthPlace
					formdata.highestEduName = d.highestEduName
					formdata.entrydate = d.entrydate
					formdata.mobile = d.mobile
					formdata.areaName = d.areaName
					formdata.idTypeName = d.idTypeName
					formdata.idcard = d.idcard
					formdata.certificateValidDate = d.certificateValidDate
					formdata.nationName = d.nationName
					formdata.marriageName = d.marriageName
					formdata.politicalName = d.politicalName
					formdata.bloodTypeName = d.bloodTypeName
					formdata.userno = d.userno
					formdata.registerCategoryName = d.registerCategoryName
					formdata.birthaddress = d.birthaddress
					formdata.currentaddress = d.currentaddress
				})
			},
			formSubmit(){
				let _that = this 
				_that.formdata.objStr =""
				_that.formdata.objStr4 =""
			for(let item of _that.subEditorObj["backLeave"].workList){
				if(!item.endTime){
					errorModal(_that,'提示信息','销假时间中的实际结束时间未填写')
					return
				}
			}
				_that.om.formSave('tbackLeave/save',_that.formdata,_that)
				
			},
			getChildData(id){
				let _that = this
				_that.om.request(
					"tbackLeave/getChildData",
					{
						"id":id
					},
				).then(res=>{
					if(res.value){
						_that.subEditorObj["takeOffice"].workList = res.value.taskLeave_workinfos
						let obj = {
							endTime1:null
						}
						for(let item of res.value.taskLeaves){
							Object.assign(item,obj)
							item.endTime1 = item.endTime
							item.endTime = ""
						}
						if(_that.subEditorObj["backLeave"]){
							_that.subEditorObj["backLeave"].workList = res.value.taskLeaves
						}
						if(_that.subEditorObj["replaceTile"]){
							_that.subEditorObj["replaceTile"].workList = res.value.taskLeave_datas
						}
					}
				})
			},
			findUserById(id){
				let _that = this
				_that.om.request(
					"tbackLeave/queryData1",
					{
						isTop: true,
						userId: id
					},
					"post"
				).then(res=>{
					if(res.rows){
						let formdata = _that.$refs.autoform.formdata
						let val = res.rows
						_that.dvids = val.map(e => e.dvid).join(",")
					}
				})
			},
			findUserById1(id){
				let _that = this
				_that.om.request(
					"temployee/findUserById",
					{
						isTop: true,
						userId: id
					}
				).then(res=>{
					if(res.value){
						let obj ={
							infoId:res.value.id,
							infoName:res.value.username
						}
						Object.assign(res.value,obj)  
						_that.$refs.autoform.formdata.objStr5 = JSON.stringify(res.value)
						_that.formdata.objStr5 = JSON.stringify(res.value)
					}
				})
			},
			
			intLoad(options){
				let _that = this
				let ajaxArr = []
				ajaxArr.push({
					url:'tbackLeave/findUserComboboxAsSameInstIsHoliday'
				})
				formModelLoad(ajaxArr,function(res,mainData){
				changeEditor("topinfo","zuserId",{
					defaultValue:_that.userId,
					defaultName:_that.userName
				},mainData)
					changeEditor("leavePersonTitle","infoId",{
						link:"../../public/pageSearch/pageSearch",
						"linkParam":{
							other:JSON.stringify({}),
							namefield:"userSalaryPosInstName",
							extrafield:"salaryPosInstName",
							searchfield: "q",
							initload:true,
							url:"tbackLeave/findUserComboboxAsSameInstIsHoliday",
							cachename:_that.infoCacheKey,
							placeholder:"请输入姓名、岗位、机构"
						}
					},null,mainData)
				if(_that.subEditorObj['takeOffice']){
					changeEditor("takeOffice","objStr2",{
						showAdd:false,
						showDel:false,
						showArrow:false,
						titleTemplate: "岗位名称:<%=posName||'空'%>",
						subtitle1Template: "机构简称:<%= instShort ||'空'%>/组织简称:<%= orgShort||'空' %>",
						subtitle2Template:"岗位职级:<%= posCareercat||'空' %>/计酬方式:<%=salaryWay||'空' %>",
						subtitle3Template:"担任方式:<%=workWayName||'空' %>"
					},null,mainData)
				}
				if(_that.subEditorObj['backLeave']){
					changeEditor("backLeave","objStr3",{
						showAdd:false,
						showDel:false,
						titleTemplate: "请假类别:<%=leaveClassName||'空'%>",
						subtitle1Template:"请假开始时间:<%=  startTime + '请假结束时间:' + endTime1 %>",
						subtitle2Template:"实际结束时间:<%=endTime||'未填写' %>"
					},null,mainData)
				}
				if(_that.subEditorObj["replaceTile"]){
					if(_that.subEditorObj["replaceTile"]){
						let att = {
							showAdd:false,
							showDel:false
						}
						let configData = _that.subConfigData["replaceTile"]
						if(hasEditor("replaceTile",'posId',configData)){
							let title = getEditorTitle("replaceTile",'posId',configData)
							att.titleTemplate = title+':<%= posName || "未填写"%>'
						}
						if(hasEditor("replaceTile",'replaceId',configData)){
							let title = getEditorTitle("replaceTile",'replaceId',configData)
							att.subtitle1Template = title+':<%= replaceName || "未填写"%>'
						}
						changeEditor("replaceTile","replaceStr",att,mainData)
					}
				}
				_that.$nextTick(function(){
					if(!res[0].length){
						errorModal(_that,'提示信息','当前没有人员可以进行销假')
					}else if(res[0].length == 1){
						_that.getEmpBasicInfo(res[0][0].id)
						_that.findUserById(res[0][0].id)
						_that.$refs.autoform.formdata.infoId = res[0][0].id
						_that.$refs.autoform.formdata.infoName = res[0][0].username
					}
				})
				},options)
			}
		},
		onUnload(){
					//#ifdef APP-PLUS
					let _that = this;
					if (_that.$refs.autoform) {
						for (var fieldId in _that.subEditorObj) {
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					}
					//#endif
				},
		onLoad(options) {
			let _that = this
			_that.intLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for (var fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							_that.findUserById1(_that.$refs.autoform.formdata.infoId)
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.infoCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.infoId = d.id
							formdata.infoName = d.username
							_that.getEmpBasicInfo(d.id)
							_that.findUserById(d.id)
							 uni.removeStorage({
							 	key:_that.infoCacheKey
							 })
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
